<?php

namespace App\Admin\Forms;

use App\Admin\Exporter\SignExporter;
use App\Hospital\Controllers\DataController;
use App\Jobs\HospitalNotice;
use Dcat\Admin\Widgets\Form;
use App\Admin\Field\IntervalWeekNum;
use App\Models\HospitalAppointInterval;
use App\Models\HospitalAppointIntervalNum;
use App\Models\HospitalAppointStreet;
use App\Models\Notice;
use App\Models\Street;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Facades\Excel;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;

class DataDownLoadForm extends Form implements LazyRenderable
{
    use LazyWidget; // 使用异步加载功能

    /**
     * Handle the form request.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {

        try {
            $url = action([DataController::class, 'download'], $input);

            return $this->response()->success('成功')->download($url);

        } catch (\Exception $exception) {
            return $this->response()->error($exception->getMessage());
        }


    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $payload = $this->payload;
        if($payload['type'] == 'guide'){
            $this->dateRange('start','end','指导日期');
        }elseif($payload['type'] == 'bigChild'){
            $this->dateRange('start', 'end', '签约日期');
            $this->radio('is_idcard', '仅导出身份证必填')->options([0 => '否', 1 => '是'])->default(1);
            $this->display("text", '注:')->value("选择身份证必填将过滤掉身份证未填写，以及不合规（位数）的数据");
            $this->radio('status', '仅导出当前为签约状态')->options([0 => '否', 1 => '是'])->default(1);
            $this->display("text", '注:')->value("选择否则会导出“已解约”的签约数据");
            $this->radio('is_admin', '仅导出管辖数据')->options([0 => '否', 1 => '是'])->default(1);
            $this->display("text", '注:')->value("根据社区提供的“计免数据”导出结果");

        }elseif($payload['type'] == 'preg'){
            $this->dateRange('start', 'end', '签约日期');
            $this->radio('is_idcard', '仅导出身份证必填')->options([0 => '否', 1 => '是'])->default(1);
            $this->display("text", '注:')->value("选择身份证必填将过滤掉身份证未填写，以及不合规（位数）的数据");
            $this->radio('status', '仅导出当前为签约状态')->options([0 => '否', 1 => '是'])->default(1);
            $this->display("text", '注:')->value("选择‘否’则会导出“已解约”的签约数据");
            $this->radio('is_admin', '仅导出管辖数据')->options([0 => '否', 1 => '是'])->default(1);
            $this->display("text", '注:')->value("根据社区提供的“孕产妇数据”导出结果");
            $this->radio('preg_status', '是否包含妊娠异常结束')->options([0 => '否', 1 => '是'])->default(0);
            $this->display("text", '注:')->value("选择‘是’则会导出用户追访时填写“自然流产、人工流产、胎停育”的数据");
        }else{
            $this->display("text", '')->value("根据社区提供的‘二期儿童’数据且年龄小于三岁的儿童导出结果");
        }
        $this->hidden('type')->value($payload['type']);
        $this->hidden('hospital_id')->value($payload['hospital_id']);

    }
}
